package com.baiyinet.ui.login;


import android.view.KeyEvent;

import com.baiyinet.MyApp;
import com.baiyinet.R;
import com.baiyinet.di.DaggerScope;
import com.baiyinet.di.Injector;
import com.baiyinet.ui.general.GeneralActivity;
import com.baiyinet.util.DoubleClickExitDetector;
import com.baiyinet.util.Utils;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EActivity;

import javax.inject.Inject;

import autodagger.AutoComponent;
import autodagger.AutoInjector;
import myandroid.library.util.ApplicationHelper;

/**
 * @author J.Huang
 * @version 1.0
 * @since 16/12/11
 */
@AutoComponent(
        dependencies = MyApp.class
)
@AutoInjector
@DaggerScope(LoginActivity.class)
@EActivity(R.layout.activity_general)
public class LoginActivity extends GeneralActivity {

    @Inject
    DoubleClickExitDetector mDoubleClickExitDetector;

    @AfterInject
    void initLoginDI() {
        Injector.initLoginActivityComponent().inject(this);
    }

    /**
     * 重定义返回键操作
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBackPressed();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
        if (mDoubleClickExitDetector.click()) {
            Utils.App.clearCache(this);
            ApplicationHelper.getInstance().exit();
            super.onBackPressed();
        }
    }

    @Background(delay = 5000)
    void delayToFinishApp() {
        Utils.App.clearCache(this);
        finish();
        ApplicationHelper.getInstance().exit();
    }
}
